from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.template import RequestContext
from django.shortcuts import render_to_response

from models import *




def index(request):
    return HttpResponseRedirect("/admin/")


@login_required
def select_project_codes_per_owner(request, id=None, template_name='blank.txt'):
   """
          select projectcodes from model return string in json format      
   """
   projectcodes = ProjectCode.objects.filter(project_code__exact=id)
   # don't use serializers like that
   #json  = serializers.serialize('json', locations, fields=('id','description'))
   # it dosn't work, this must be removed []
   data = {
                "projectcodes":projectcodes
            }
   
   response = render_to_response(template_name,
                             data,
                             context_instance=RequestContext(request),
                             mimetype="application/json")
   response.__setitem__('Cache-Control', 'no-store,no-cache')
   response.__setitem__('Pragma', 'no-cache')
   response.__setitem__('Expires', '-1')     
   return response


@login_required
def filter_projects(request, id=None, template_name='blank.txt'):
   """
      select projectcodes from database return string in json format      
   """
   projectcodes = ProjectCode.objects.filter(project_code__exact=id)
   # don't use serializers like that
   #json  = serializers.serialize('json', locations, fields=('id','description'))
   # it dosn't work, this must be removed []
   data = {
                "projectcodes":projectcodes
            }
   
   response = render_to_response(template_name,
                             data,
                             context_instance=RequestContext(request),
                             mimetype="application/json")
   response.__setitem__('Cache-Control', 'no-store,no-cache')
   response.__setitem__('Pragma', 'no-cache')
   response.__setitem__('Expires', '-1')     
   return response





